﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>btable</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/css/btable.css" />
    <script src="/static/js/inout/jquery-1.8.2.min.js"></script>
</head>

<body style=" background-color: gainsboro;">

    <div style="margin:0px; background-color: white; margin:0 10px;">
        <blockquote class="layui-elem-quote" style="height: 31px;">
            <button type="button" class="layui-btn layui-btn-small" id="add"><i class="fa fa-plus" aria-hidden="true"></i>添加</button>
            <!--<button id="add" class="layui-btn">向选项卡添加一个tab</button>-->
            <form class="layui-form" style="float:right;" action="/admin/user/bicthmomo" method="post">
                <div class="layui-form-item" style="margin:0;">
                    <label class="layui-form-label">搜索</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="输入用户名" value="" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux" style="padding:0;">
                        <button lay-filter="search" class="layui-btn" lay-submit type="submit"><i class="fa fa-search" aria-hidden="true"></i> 查询</button>
                    </div>
                </div>
            </form>
        </blockquote>
        <div id="content" style="width: 100%;height: 533px;"></div>
        <div id="demo7"></div>
    </div>

    <script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/static/js/',
            v: new Date().getTime()
        }).use(['btable', 'form','paging'], function () {
            var btable = layui.btable(),
                $ = layui.jquery,
                layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
                layer = layui.layer,//获取当前窗口的layer对象;
                form = layui.form();

            function varthis(_this) {

            }


            btable.set({
                openWait: true,//开启等待框
                elem: '#content',
                url: '/admin/user/bicthmomo', //数据源地址
                pageSize: 10,
                params: {
                    t: new Date().getTime() //额外的请求参数
                },
                columns: [{ //配置数据列
                    fieldName: '用户名', //显示名称
                    field: 'username', //字段名
                    sortable: true //是否显示排序
                }, {
                    fieldName: '用户角色',
                    field: 'title',
                    sortable: true
                },{
                    fieldName: '职位',
                    field: 'ztet',
                    sortable: true
                },{
                    fieldName: '创建时间',
                    field: 'createTime',
                    sortable: true
                },{
                    fieldName: '上次登录时间',
                    field: 'loginTime',
                    sortable: true
                },{
                    fieldName: '登陆ip',
                    field: 'loginIp',
                    sortable: true
                },{
                    fieldName: '在线状态',
                    field: 'stype',
                    sortable: true
                },{
                    fieldName: '账号状态',
                    field: 'stuts',
                    format: function (id, obj) {
                        //id
                        //行数据对象
                        //返回值：格式化的纯文本或html文本
                        return obj.sign;
                    }
                }, {
                    fieldName: '操作',
                    field: 'id',
                    format: function (val,obj) {
                        var po = '';
                        if(obj.disable==1){
                            po="禁用";
                        }else{
                            po="启用";
                        }
                        var html = '<input type="button" value="修改" id="edit" data-action="edit" data-id="'+val+'" class="layui-btn layui-btn-mini" /><button data-id="'+val+'" data-bd="'+obj.disable+'" class="layui-btn layui-btn-normal layui-btn-mini ble" onclick="defa(this)">'+po+'</button>' +
                            '<input type="button" value="删除" data-action="del" data-id="'+val+'" class="layui-btn layui-btn-mini layui-btn-danger del" name="del" />' +
                            '<input type="button" data-id="'+val+'" class="layui-btn layui-btn-normal layui-btn-mini" data-action="fzlx" value="负责类型"/><input type="button" value="手册权限" id="getall" data-action="getall" data-id="'+val+'" class="layui-btn layui-btn-mini" />{if condition="$useid eq 1"}<input type="button" class="layui-btn layui-btn-normal layui-btn-mini ble" data-bd="'+obj.phone+'" data-id="'+val+'" data-action="kiphone" value="查看联系方式"/>{/if}';
                        return html;
                    }
                }],
                even: true,//隔行变色
                field: 'id', //主键ID
                //skin: 'row',
                checkbox: false,//是否显示多选框
                paged: true, //是否显示分页
                singleSelect: false, //只允许选择一行，checkbox为true生效
                onSuccess: function ($elem) { //$elem当前窗口的jq对象
                    $elem.children('tr').each(function () {
                        $(this).children('td:last-child').children('input').each(function () {
                            var $that = $(this);
                            var action = $that.data('action');
                            var uid = $that.data('id');
                            var phone = $that.data('bd');
                            $that.on('click', function () {
                                switch (action) {
                                    case 'edit':
                                            var cons = '<div style="padding:10px;"><input type="hidden" value="'+uid+'" class="layui-input" style="margin-bottom:5px;" id="uid"/>' +
                                                    '<input type="password" class="layui-input" style="margin-bottom:5px;" id="password" placeholder="新密码" /><input type="password" class="layui-input" style="margin-bottom: 5px;" id="newpassword" placeholder="确认密码"/></div>';
                                            layer.open({
                                                type: 1,
                                                title: '修改用户',
                                                content: cons,
                                                btn: ['确定', '取消'],
                                                area: ['350px', '210px'],
                                                yes: function (index, layero) {
                                                    //这是核心的代码。
                                                    var uid = $("#uid").val();
                                                    var password = $("#password").val();
                                                    var newpassword = $("#newpassword").val();
                                                    if(password==''){
                                                        layer.msg("密码不能为空");
                                                        return false;
                                                    }
                                                    if(newpassword==""){
                                                        layer.msg("请确认密码");
                                                        return false;
                                                    }
                                                    if(password!=newpassword){
                                                        layer.msg("密码不一致");
                                                        return false;
                                                    }
                                                    $.ajax({
                                                        type:"post",
                                                        dataType: "json",
                                                        url: "/admin/user/edit",
                                                        data:{
                                                            password:password,
                                                            newpassword:newpassword,
                                                            uid:uid
                                                        },
                                                        success:function(res){
                                                            if(res.status==0){
                                                                layer.msg(res.msg);
                                                                setTimeout('location.href="/admin/user/index"',1000);
                                                            }else{
                                                                layer.msg(res.msg);
                                                            }
                                                        }
                                                    });

                                                },
                                                maxmin: true
                                            });
                                        break;
                                    case 'del': //删除
                                        //询问框
                                        layer.confirm('确定要删除？', {
                                            btn: ['确定','取消'] //按钮
                                        }, function(){
                                            var uid = $that.attr('data-id');
                                            $.ajax({
                                                type:"post",
                                                dataType: "json",
                                                url: "/admin/user/del",
                                                data:{
                                                    uid:uid
                                                },
                                                success:function(res){
                                                    if(res.status==0){
                                                        layer.msg(res.msg,{icon: 1});
                                                        setTimeout('location.href="/admin/user/index"',1000);
                                                    }else{
                                                        layer.msg(res.msg,{icon: 1});
                                                    }
                                                }
                                            });
                                        }, function(){

                                        });
                                        break;
                                    case 'fzlx':
                                        var cons = '<form class="layui-form" id="ops"><div class="layui-form-item"><div class="layui-input-block" style="margin-left: 5px;"><input type="hidden" value="'+uid+'" class="layui-input" style="margin-bottom:5px;" id="uid"/>' +
                                                '{volist name="vers" id="ic"}<input type="checkbox" name="like{$i}" class="vars" title="{$ic.titlename}" value="{$ic.id}">{/volist}</div></div></form>';
                                        var id = $(this).attr("data-id");
                                        layer.open({
                                            type: 1,
                                            title: '负责类型',
                                            content: cons,
                                            btn: ['确定', '取消'],
                                            area: ['350px', '350px'],
                                            success:function(index, layero){
                                                $.ajax({
                                                    type:"post",
                                                    dataType: "json",
                                                    url: "/admin/user/vartype",
                                                    data:{
                                                        id:id
                                                    },
                                                    success:function(res){
                                                        var data = res;
                                                        $.each($('.vars'),function(index,contents){
                                                            var p = $.inArray(contents.value,data);
                                                            if(p>=0){
                                                                $(this).attr('checked',true);
                                                                form.render();
                                                            }
                                                        })
                                                    }
                                                });

                                                form.render();
                                            },
                                            yes: function (index, layero) {
                                                //这是核心的代码。
                                                var uid = $("#uid").val();
                                                var data = $("#ops").serialize();
                                                $.ajax({
                                                    type:"post",
                                                    dataType: "json",
                                                    url: "/admin/user/versio",
                                                    data:{
                                                        data:data,
                                                        uid:uid
                                                    },
                                                    success:function(res){
                                                        if(res.status==0){
                                                            layer.msg(res.msg);
                                                            setTimeout('location.href="/admin/user/index"',1000);
                                                        }else{
                                                            layer.msg(res.msg);
                                                        }
                                                    }
                                                });
                                            },
                                            maxmin: true
                                        });
                                        break;
                                    case 'getall':
                                        var cony = '<form class="layui-form" id="ivbe"><div class="layui-form-item"><div class="layui-input-block" style="margin-left: 5px;"><input type="hidden" value="'+uid+'" class="layui-input" style="margin-bottom:5px;" id="uid"/>' +
                                                '{volist name="inty" id="in"}<input type="checkbox" name="liko{$i}" class="intyu" title="{$in.title}" value="{$in.id}">{/volist}</div></div></form>';
                                        var ids = $(this).attr("data-id");
                                        layer.open({
                                            type: 1,
                                            title: '手册分类',
                                            content: cony,
                                            btn: ['确定', '取消'],
                                            area: ['350px', '350px'],
                                            success:function(index, layero){
                                                $.ajax({
                                                    type:"post",
                                                    dataType: "json",
                                                    url: "/admin/user/intsy",
                                                    data:{
                                                        ids:ids
                                                    },
                                                    success:function(res){
                                                        var data = res;
                                                        $.each($('.intyu'),function(index,contents){
                                                            var p = $.inArray(contents.value,data);
                                                            if(p>=0){
                                                                $(this).attr('checked',true);
                                                                form.render();
                                                            }
                                                        })
                                                    }
                                                });
                                                form.render();
                                            },
                                            yes: function (index, layero) {
                                                //这是核心的代码。
                                                var uid = $("#uid").val();
                                                var data = $("#ivbe").serialize();
                                                $.ajax({
                                                    type:"post",
                                                    dataType: "json",
                                                    url: "/admin/user/intyer",
                                                    data:{
                                                        data:data,
                                                        uid:uid
                                                    },
                                                    success:function(res){
                                                        if(res.status==0){
                                                            layer.msg(res.msg);
                                                            setTimeout('location.href="/admin/user/index"',1000);
                                                        }else{
                                                            layer.msg(res.msg);
                                                        }
                                                    }
                                                });
                                            },
                                            maxmin: true
                                        });
                                        break;
                                    case 'kiphone':
                                        var bodes = '<form class="layui-form" style="height:100%;"><div class="layui-form-item" style="text-align: center;margin:0;height: 100%;overflow: hidden;line-height: 3;">' +
                                                '<i class="layui-icon" style="font-size: 30px; color: #1E9FFF;">&#xe63b;</i><span style="display: -webkit-inline-box;position: relative;top:-7px;">：</span>' +
                                                '<span style="display: -webkit-inline-box;position: relative;top:-7px;font-size: 22px;font-family:Microsoft YaHei;">'+phone+'</span></div></form>';
                                        layer.open({
                                            type: 1,
                                            title: '联系方式',
                                            content: bodes,
                                            btn: ['关闭'],
                                            area: ['300px', '180px'],
                                            maxmin: true
                                        });
                                        break;

                                }
                            });
                        });
                    });
                }
            });

            //添加用户窗口
            btable.render();
            //监听搜索表单的提交事件
            form.on('submit(search)', function (data) {
                btable.get(data.field);
                return false;
            });
            $(window).on('resize', function (e) {
                var $that = $(this);
                $('#content').height($that.height() - 92);
            }).resize();
        });
        //上面那个是页面渲染完就添加的
        layui.use(['layer','form'], function () {
            var $ = layui.jquery,
                    layer = layui.layer,
                    form = layui.form();

            //移除当前的选项卡
            $('#remove').on('click', function () {
                var tab = parent.tab;

                tab.deleteTab(tab.getCurrentTabId());
            });

            //这个是点击之后才添加的。
            $('#add').on('click', function () {
                var con = '<div style="padding:10px;"><input type="text" class="layui-input" style="margin-bottom:5px;" id="username" placeholder="用户名" />' +
                        '<input type="password" class="layui-input" style="margin-bottom:5px;" id="password" placeholder="密码" /><input type="password" class="layui-input" style="margin-bottom: 5px;" id="newpassword" placeholder="确认密码"/>' +
                        '<input type="text" class="layui-input" id="name" placeholder="真实姓名" value=""/><input type="text" class="layui-input" id="phone" placeholder="联系方式" value=""/>' +
                        '<select id="sole" class="layui-input" style="margin-bottom:5px;text-align: center;"><option value="">选择角色</option>{volist name="loru" id="io"}<option value="{$io.id}">{$io.title}</option>{/volist}</select>' +
                        '<select id="position" class="layui-input" style="margin-bottom:5px;text-align: center;"><option value="">选择职位</option>{if condition="$position eq 0"}<option value="0">管理员</option>{/if}<option value="1">技术</option><option value="2">测试</option><option value="3">主管</option><option value="4">客服</option><option value="5">行政</option></select></div>';

                layer.open({
                    type: 1,
                    title: '用户添加',
                    content: con,
                    btn: ['确定', '取消'],
                    area: ['350px', '420px'],
                    yes: function (index, layero) {
                        //这是核心的代码。
                        var username = $("#username").val();
                        var password = $("#password").val();
                        var newpassword = $("#newpassword").val();
                        var sole = $("#sole").val();
                        var position = $("#position").val();
                        var names = $("#name").val();
                        var phone = $("#phone").val();
                        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                        if(username==''){
                            layer.msg("用户名不能为空");
                            return false;
                        }
                        if(password==""){
                            layer.msg("密码不能为空");
                            return false;
                        }
                        if(newpassword==""){
                            layer.msg("请确认密码");
                            return false;
                        }
                        if(password!=newpassword){
                            layer.msg("密码不一致");
                            return false;
                        }
                        if(!myreg.test(phone))
                        {
                            layer.msg("请输入有效的手机号码！");
                            return false;
                        }
                        if(sole==""){
                            layer.msg("请设置用户角色");
                            return false;
                        }
                        if(position==""){
                            layer.msg("请设置用户职位");
                            return false;
                        }
                        $.ajax({
                            type:"post",
                            dataType: "json",
                            url: "/admin/user/add",
                            data:{
                                username:username,
                                password:password,
                                   names:names,
                                    sole:sole,
                                   phone:phone,
                                position:position
                            },
                            success:function(res){
                                if(res.status==0){
                                    layer.msg(res.msg);
                                    setTimeout('location.href="/admin/user/index"',1000);
                                }else{
                                    layer.msg(res.msg);
                                }
                            }
                        });
                    },
                    maxmin: true
                });

            });
            function clatype(_this) {

            }

        });
function defa(_this) {
    var m = $(_this).attr("data-bd")==1?'您是否要禁用该账户?':'您是否要启用该账户?';
    layer.confirm(m,{
        btn: ['确定','取消'] //按钮
    }, function(){
        var id = $(_this).attr("data-id");
        $.ajax({
            type:"post",
            dataType: "json",
            url: "/admin/user/jinyong",
            data:{
                id:id
            },
            success:function(res){
                if(res.status==0){
                    layer.msg(res.msg);
                    setTimeout('location.href="/admin/user/index"',1000);
                }else{
                    layer.msg(res.msg);
                }
            }
        });
    }, function(){
    });
}

    </script>
</body>

</html>